<template>
  <div>
    <avue-crud
      :option="optionDispenserList"
      :table-loading="loading"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot-scope="{ row }" slot="menuRight">
        <el-button type="primary" size="small" @click="addNew"
          >新增加注机</el-button
        >
      </template>
      <template slot-scope="{ row }" slot="menuBtn">
        <el-dropdown-item divided @click.native="handleCommand(row)"
          >查看</el-dropdown-item
        >
        <el-dropdown-item
          divided
          @click.native="install(row)"
          type="text"
          size="small"
          >安装</el-dropdown-item
        >
        <el-dropdown-item
          divided
          @click.native="deleteRow(row.$index, row)"
          type="text"
          size="small"
          >删除</el-dropdown-item
        >
        <el-dropdown-item divided @click.native="zoomInImg(row.macQrcode)"
          >查看二维码</el-dropdown-item
        >
      </template>
    </avue-crud>
    <el-dialog
      title="安装加注机"
      append-to-body
      :visible.sync="showInstallDeviceFlag"
      width="600px"
    >
      <dispenser-install-content
        :machine-id="currentFillingMachineId"
        v-if="showInstallDeviceFlag"
        @hide="hideInstall"
      />
    </el-dialog>
    <el-dialog
      title="拆除加注机"
      append-to-body
      :visible.sync="showRemoveDeviceFlag"
      width="600px"
    >
      <dispenser-remove-content
        :machine-id="currentFillingMachineId"
        v-if="showRemoveDeviceFlag"
        :site-name="currentSiteName"
        @hide="hideRemove"
      />
    </el-dialog>
    <el-dialog
      title="查看二维码"
      append-to-body
      :visible.sync="showQrcodeFlag"
      width="400px"
    >
      <div class="flex-center">
        <div></div>
        <div id="qrBig" ref="qrBig"></div>
        <div></div>
      </div>
      <div class="m-t-10" style="text-align: center">
        <el-button type="primary" @click="downloadQrImg" size="small"
          >下载</el-button
        >
        <el-button type="primary" @click="downloadQrImg" size="small"
          >同步到设备</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
import { getList } from "@/api/stake/dispenser";
import dispenserInstallContent from "@/views/stake/dispenser-install-content.vue";
import dispenserRemoveContent from "@/views/stake/dispenser-remove-content.vue";
import { mapGetters } from "vuex";
import { optionDispenserList } from "@/const/stake/dispenser";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionDispenserList,
      data: [{}],
      showInstallDeviceFlag: false,
      currentFillingMachineId: "",
      currentSiteName: "",
      showRemoveDeviceFlag: false,
      showQrcodeFlag: false,
      qr2: null,
      showBatchSetPriceFlag: false,
      global: {
        unitPrice: 0,
      },
    };
  },
  components: {
    dispenserInstallContent,
    dispenserRemoveContent,
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {};
    },
  },
  methods: {
    refresh() {
      this.onLoad(this.page);
    },
    handleCommand(row) {
      console.log(row);
      this.$emit("detail", row);
      // if(command==='viewDetail'){
      //   this.btnName='查看'
      // }else if(command==='installation'){
      //   this.btnName='安装'
      // }else if(command==='delete'){
      //   this.btnName='删除'
      // }else if(command==='viewQrcode'){
      //   this.btnName=='查看二维码'
      // }
    },
    install(row) {
      console.log(row);
      this.showInstallDeviceFlag = true;
      this.currentFillingMachineId = row.id;
    },
    hideInstall(flag) {
      this.showInstallDeviceFlag = false;
      if (flag) this.query();
    },
    deleteRow(index, row) {
      this.showRemoveDeviceFlag = true;
      this.currentFillingMachineId = row.id;
      this.currentSiteName = row.siteName;
    },
    hideRemove(flag) {
      this.showRemoveDeviceFlag = false;
      if (flag) this.query();
    },
    zoomInImg(macQrCode) {
      console.log(macQrCode);
      this.showQrcodeFlag = true;
      setTimeout(() => {
        if (this.qr2 == null) {
          this.qr2 = new QRCode(this.$refs.qrBig, {
            text: macQrCode,
            width: 300,
            height: 300,
            colorDark: "#333333", //二维码颜色
            colorLight: "#ffffff", //二维码背景色
            correctLevel: QRCode.CorrectLevel.L, //容错率，L/M/H
          });
        }
      }, 400);
    },
    downloadQrImg() {
      // if (isStringEmpty(this.form.macQrCode)) return msgError("二维码无效")
      // downloadBase64File("加注机二维码.png", this.form.macQrCode)
      let dom = document.getElementById("qrBig");
      // 获取dom二维码图片的连接
      let url = dom.childNodes[1].src; //codeIMG  要下载的路径
      let link = document.createElement("a");
      // 这里是将url转成blob地址，
      fetch(url)
        .then((res) => res.blob())
        .then((blob) => {
          // 将链接地址字符内容转变成blob地址
          link.href = URL.createObjectURL(blob);
          link.download = "二维码";
          document.body.appendChild(link);
          link.click();
        });
    },
    //新增加注机
    addNew() {
      this.$emit("add");
    },
    //单价批量设置
    batchSetPrice() {
      if (this.selectionList.length === 0) {
        this.$message({
          type: "warning",
          message: "请选择加注机",
        });
      } else {
        this.showBatchSetPriceFlag = true;
      }
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.data.forEach((item) => {
          item.macQrcode =
            "https://mp.weixin.qq.com/a/~~YZAJL3Lklec~zkfN_-S0BUBAS3spynnl4A~~";
        });
        console.log(this.data);
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
